package com.bridgeintelligent.tag.webserver.dim.mgmt.service.report;

import com.bridgeintelligent.tag.webserver.dim.mgmt.api.report.ReportHotDimInfo;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.List;

/**
 * @Author：liwei
 * @Date：2023/9/28 15:39
 * @Desc：
 */
public class DataSorter {

    public static void setScoreST(List<ReportHotDimInfo> datas){
        //归一化算法：X-min/max-min
//        List<Integer> useNumbers = datas.stream().map(ReportHotDimInfo::getUseNum).collect(Collectors.toList());
//        Integer useNumberMax = Collections.max(useNumbers);
//        Integer useNumberMin = Collections.min(useNumbers);
//        List<Integer> userNumbers = datas.stream().map(ReportHotDimInfo::getUserNum).collect(Collectors.toList());
//        Integer userNumberMax = Collections.max(userNumbers);
//        Integer userNumberMin = Collections.min(userNumbers);

        Integer useNumberMax = datas.stream().max(Comparator.comparingInt(ReportHotDimInfo::getUseNum)).get().getUseNum();
        Integer useNumberMin = datas.stream().min(Comparator.comparingInt(ReportHotDimInfo::getUseNum)).get().getUseNum();
        Integer userNumberMax = datas.stream().max(Comparator.comparingInt(ReportHotDimInfo::getUserNum)).get().getUserNum();
        Integer userNumberMin = datas.stream().min(Comparator.comparingInt(ReportHotDimInfo::getUserNum)).get().getUserNum();
        Integer downNumberMax = datas.stream().max(Comparator.comparingInt(ReportHotDimInfo::getDownloadNum)).get().getDownloadNum();
        Integer downNumberMin = datas.stream().min(Comparator.comparingInt(ReportHotDimInfo::getDownloadNum)).get().getDownloadNum();
        DecimalFormat df = new DecimalFormat("0.0000");
        if(userNumberMax-userNumberMin==0&&useNumberMax-useNumberMin==0&&downNumberMax-downNumberMin==0){
            datas.forEach(e->e.setScoreST(0.0000));
        }else if(userNumberMax-userNumberMin==0&&useNumberMax-useNumberMin!=0&&downNumberMax-downNumberMin!=0){
            datas.forEach(e->e.setScoreST(Double.valueOf(df.format((((float)(e.getUseNum() - useNumberMin) / (useNumberMax - useNumberMin)) + ((float)(e.getDownloadNum() - downNumberMin) / (downNumberMax - downNumberMin)))))));
        }else if(useNumberMax-useNumberMin==0&&userNumberMax-userNumberMin!=0&&downNumberMax-downNumberMin!=0){
            datas.forEach(e->e.setScoreST(Double.valueOf(df.format((((float)(e.getUserNum() - userNumberMin) / (userNumberMax - userNumberMin)) + ((float)(e.getDownloadNum() - downNumberMin) / (downNumberMax - downNumberMin)))))));
        }else if(downNumberMax-downNumberMin==0&&useNumberMax-useNumberMin!=0&&userNumberMax-userNumberMin!=0){
            datas.forEach(e->e.setScoreST(Double.valueOf(df.format((((float)(e.getUserNum() - userNumberMin) / (userNumberMax - userNumberMin)) + ((float)(e.getUseNum() - useNumberMin) / (useNumberMax - useNumberMin)))))));
        }else {
            datas.forEach(e->e.setScoreST(Double.valueOf(df.format((
                    ((float)(e.getUseNum() - useNumberMin) / (useNumberMax - useNumberMin))
                    + ((float)(e.getUserNum() - userNumberMin) / (userNumberMax - userNumberMin))
                    + ((float)(e.getDownloadNum() - downNumberMin) / (downNumberMax - downNumberMin))
            )))));
        }
//        List<Double> originalScoreSTs = datas.stream().map(ReportHotDimInfo::getScoreST).collect(Collectors.toList());
//        Double originalScoreSTMax = Collections.max(originalScoreSTs);
//        Double originalScoreSTMin = Collections.min(originalScoreSTs);
        Double originalScoreSTMax = datas.stream().max(Comparator.comparingDouble(ReportHotDimInfo::getScoreST)).get().getScoreST();
        Double originalScoreSTMin = datas.stream().min(Comparator.comparingDouble(ReportHotDimInfo::getScoreST)).get().getScoreST();
        if(Math.abs(originalScoreSTMax-originalScoreSTMin-0.00) < 1e-6){
            datas.forEach(e->e.setScoreST(0.00));
        }else{
            datas.forEach(e->e.setScoreST((new BigDecimal((float)(e.getScoreST() - originalScoreSTMin)/(originalScoreSTMax - originalScoreSTMin)*100).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue())));
        }
    }

    public static void main(String[] args) {
        //多余的0不显示
//        0.6229000091552734375
//        0.771200001239776611328125
//        float f1 =((float)(1 - 0)/(2 - 0))*100;
//        BigDecimal decimal = new BigDecimal(45.0);
//        System.out.println(decimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());// 2.20
    }
}
